#!/usr/bin/env bash
set -euo pipefail

FNAME="${1:-}"
if [ -z "$FNAME" ]; then
  echo "Usage: $0 <voiceprint_XXXXXXXXXXXX.wav>"
  exit 1
fi

SERIAL=$(for s in $(adb devices | awk 'NR>1 && $2=="device"{print $1}'); do adb -s "$s" shell pm path com.roger.homecenterscreen >/dev/null 2>&1 && echo "$s" && break; done)
if [ -z "${SERIAL}" ]; then
  echo "No device with package com.roger.homecenterscreen found"
  exit 1
fi
echo "Using device: ${SERIAL}"

REMOTE="/data/data/com.roger.homecenterscreen/cache/voice/records/${FNAME}"
adb -s "${SERIAL}" exec-out run-as com.roger.homecenterscreen cat "${REMOTE}" > "./${FNAME}"
echo "Pulled to ./${FNAME}"